<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/16
 * Time: 下午2:07
 */

namespace app\merchant\controller;

use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\Merchant;
use think\Controller;
use think\Debug;
use think\Request;
use think\Session;

class Machextend extends Controller
{

    public function getqrcode()
    {
        $jg = action('Qrcodes/qrcode', array(request()->param('mid'), "https://" . $_SERVER['HTTP_HOST'] . "/index.php?s=/Index/pay/id/" . request()->param('mid') . ".html"));
        $qr = $jg;
        $length = filesize($qr);
        $type = mime_content_type($qr);
        $showname = ltrim(strrchr($qr, '/'), '/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
            header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $showname . '"');
        }
        readfile($qr);
        exit;
    }

    public function getmachqrcode(Request $request){
        if ($request->isAjax()){

            $param = $request->post();
            $Qrcodes = new Qrcodes();
            $CardQrcode = new CardQrcode();
            $CardMember = new CardMember();
            $domain_url = $request->domain();
            switch ($param['type']){
                case "pay":
                    $re_data['info']['qrcode'] = $Qrcodes->qrcode_mach($param['id'],$domain_url);;
                    $re_data['info']['qrcode_down'] = '/qrcode/paybg/'.'bg_'.$param['id'].'.png';
                    $re_data['info']['bg_qrcode'] = $Qrcodes->getmachpaybgimg($param['id']);
                    $re_data['info']['bg_qrcode_down'] = $Qrcodes->getafprint($re_data['info']['bg_qrcode'],$param['id'],'pay');
                    $re_data['return_code'] = "SUCCESS";
                    $re_data['return_msg'] = '成功';
                    break;
                case "cardus":
                    //查询是否创建了会员卡
                    $cardusinfo = $CardMember->where(['top_mid'=>Session::get('login_top_mid'),'is_del'=>1])->find();
//                    $cardusinfo['sql'] = $CardMember->getLastSql();
//                    return json($cardusinfo);
                    if ($cardusinfo) {
                        //查看是否有此门店的二维码
                        $map['outer_str'] = $param['id'];
                        $map['type'] = 1;
                        $map['url_mobile'] = ['neq',""];
                        $qrcode = $CardQrcode->where($map)->find();
                        if (!$qrcode) {
                            //未创建时查看此会员卡是否开启一键开卡
                            if ($cardusinfo['activate'] == 0) {

                                $re_data['return_code'] = "SUCCESS";
                                $re_data['return_msg'] = '您没有开启一键开卡，请开启后再点击查看';
                                $re_data['info']['qrcode'] = '/static/img/cardusbizonekey.png';
                                $re_data['info']['qrcode_down'] = '/static/img/notshow.png';
                                $re_data['info']['bg_qrcode'] = '/static/img/cardusbizonekey_bg.png';
                                $re_data['info']['bg_qrcode_down'] = '/static/img/notshow.png';
                            }else{
                                //创建此门店的开卡码
                                $extdata['outer_str'] = $param['id'];
                                $extdata['c_id'] = $cardusinfo['id'];
                                $Cardusextend = new Cardusextend();
                                $res = $Cardusextend->cardusbizadd($extdata);
                                if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'FAIL') {
                                    $re_data['return_code'] = "FAIL";
                                    $re_data['return_msg'] = $res['result_msg'];
                                }
                            }
                            $qrcode = $CardQrcode->where($map)->find();
                        }
                        //是否已有结果  无结果时创建带背景的二维码
                        if (!isset($re_data)){
                            if (empty($qrcode['qrcode_url'])){
                                action('Qrcodes/cardbizqrcode', array($qrcode['id'], $qrcode['url_mobile'] . ".html"));
                                $qrcode = $CardQrcode->where($map)->find();
                            }
                            $re_data['info']['qrcode'] = '/bizcard/' . $qrcode['id'] . '.png';
//                            $re_data['info']['qrcode'] = $qrcode['qrcode_url'];
                            $re_data['info']['qrcode_down'] = '/bizcard/' . $qrcode['id'] . '.png';
//                            $re_data['info']['qrcode_down'] = $qrcode['qrcode_url'];
                            $re_data['info']['bg_qrcode'] = $Qrcodes->getmachcardbgimg($param['id']);
                            $re_data['info']['bg_qrcode_down'] = $Qrcodes->getafprint($re_data['info']['bg_qrcode'], $param['id'], 'cardus');
                            $re_data['return_code'] = "SUCCESS";
                            $re_data['return_msg'] = '成功';
                        }
                    }else{
                        $re_data['return_code'] = "SUCCESS";
                        $re_data['return_msg'] = '未创建会员卡';
                        $re_data['info']['qrcode'] = '/static/img/notcardus.png';
                        $re_data['info']['qrcode_down'] = '/static/img/notshow.png';
                        $re_data['info']['bg_qrcode'] = '/static/img/notcardus_bg.png';
                        $re_data['info']['bg_qrcode_down'] = '/static/img/notshow.png';
                    }

                    break;
                default:
                    $re_data['info'] = '';
                    $re_data['return_code'] = "FAIL";
                    $re_data['return_msg'] = '失败';
                    break;
            }

            return json($re_data);
        }
    }

    public function getmercode()
    {
        $map['id'] = request()->param('mid');

        $Merchant = new Merchant();
        $merchant = $Merchant->where($map)->value('merchant');
        $jg = action('Qrcodes/mercode', array(request()->param('mid'), 48, "https://" . $_SERVER['HTTP_HOST'] . "/index.php?s=/Index/pay/id/" . request()->param('mid') . ".html", $merchant));
        $qr = $jg;
        $length = filesize($qr);
        $type = mime_content_type($qr);
        $showname = ltrim(strrchr($qr, '/'), '/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
            header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $showname . '"');
        }
        readfile($qr);
        exit;
    }

    public function get_mer_code(){
        $mid = request()->param('mid');
        $url = '/qrcode/'.$mid.'.png';
        return $url;
    }


}